﻿(function () {
    appModule.controller("news.views.index",
        ['$scope', '$stateParams', '$uibModel', 'uiGridConstants', 'abp.services.app.news'],
        function ($scope, $stateParams, $uibModel, uiGridConstants, newsService) {
            var vm = this;

            $scope.$on("$viewContentLoaded", function () {
                App.initAjax();
            });
            vm.loading = false;
            vm.permission = {
                create: abp.auth.hasPermission('Pages.News.CreateNews'),
                edit: abp.auth.hasPermission("Pages.News.EditNews"),
                'delete': abp.auth.hasPermission("Pages.News.DeleteNews")
            };

            var requestParams = {
                skipCount: 0,
                maxResultCount: app.consts.grid.defaultPageSize,
                sorting: null
            };

            vm.newsGridOptions = {
                enableHorizontalScrollbar: uiGridConstants.scrollbars.WHEN_NEEDED,
                enableVerticalScrollbar: uiGridConstants.scrollbars.WHEN_NEEDED,
                paginationPageSizes: app.consts.grid.defaultPageSizes,
                paginationPageSize: app.consts.grid.defaultPageSize,
                useExternalPagination: true,
                useExternalSorting: true,
                appScopeProvider: vm,
                columnDefs: [
                    {
                        name: app.localize('Actions'),
                        enableSorting: false,
                        width: 200,
                        cellTemplate:
                        '<div class=\"ui-grid-cell-contents\">' +
                        '  <div class="btn-group dropdown" uib-dropdown="" dropdown-append-to-body>' +
                        '    <button class="btn btn-xs btn-primary blue" uib-dropdown-toggle="" aria-haspopup="true" aria-expanded="false"><i class="fa fa-cog"></i> ' + app.localize('Actions') + ' <span class="caret"></span></button>' +
                        '    <ul uib-dropdown-menu>' +
                        '      <li><a ng-if="grid.appScope.permissions.edit" ng-click="grid.appScope.editNews(row.entity)">' + app.localize('Edit') + '</a></li>' +
                        '      <li><a ng-if="grid.appScope.permissions.delete" ng-click="grid.appScope.deleteNews(row.entity)">' + app.localize('Delete') + '</a></li>' +
                        '    </ul>' +
                        '  </div>' +
                        '</div>'
                    },
                    {
                        name: app.localize('Title'),
                        field: 'title'
                    },
                    {
                        name: app.localize('Content'),
                        field: 'content'
                    },
                    {
                        name: app.localize('Author'),
                        field: 'author'
                    },
                    {
                        name: app.localize('PublishedTime'),
                        field: 'publishedTime',
                        cellFilter: 'momentFormat: \'L\''
                    }
                ],
                onRegisterApi: function (gridApi) {
                    $scope.gridApi = gridApi;
                    $scope.gridApi.core.on.sortChanged($scope, function (grid, sortColumns) {
                        if (!sortColumns.length || !sortColumns[0].field) {
                            requestParams.sorting = null;
                        } else {
                            requestParams.sorting = sortColumns[0].field + ' ' + sortColumns[0].sort.direction;
                        }

                        vm.getTenants();
                    });
                    gridApi.pagination.on.paginationChanged($scope, function (pageNumber, pageSize) {
                        requestParams.skipCount = (pageNumber - 1) * pageSize;
                        requestParams.maxResultCount = pageSize;

                        vm.getNews();
                    });
                },
                data: []
            };
            //if (!vm.permissions.edit && !vm.permissions.delete) {
            //    vm.newsGridOptions.columnDefs.shift();
            //};
            vm.getNews = function () {
                vm.loading = true;
                newsService.GetPagedNewssAsync({
                    "filterText": vm.filterText,
                    "sorting": requestParams.sorting,
                    "maxResultCount": requestParams.maxResultCount,
                    "skipCount": requestParams.skipCount
                }).then(function (result) {
                    vm.newsGridOptions.totalItems = result.data.totalCount;
                    vm.newsGridOptions.data = result.data.items;

                }).finally(function () {
                    vm.loading = false;
                });
            };
            vm.createNews = function () {

            };
            vm.editNews = function () { };
            vm.deleteNews = function () { };
            vm.getNews();
        });
})();